package adminlogdao

import (
	"vpn-management-server/logger"
	"vpn-management-server/src/dao"
	"vpn-management-server/src/model/adminlogmodel"
)

func AddAdminLogs(adminLogs []*adminlogmodel.AdminLog) (int, error) {
	tx, _ := dao.GMysqlDB.Conn.Begin()
	for i, Item := range adminLogs {
		sqlStr := "INSERT INTO admin_log(name,source,location,behavior,timestamp) value(?,?,?,?,?)"
		_, err := tx.Exec(sqlStr, Item.Name, Item.Source, Item.Location, Item.Behavior, Item.TimeStamp)
		if err != nil {
			logger.Logger.Error(err)
			tx.Rollback()
			return i, err
		}
	}
	return -1, tx.Commit()
}

func GetAllAdminLogs(pageSize int, pageNo int, beginTime string, endTime string) []*adminlogmodel.AdminLog {
	start := pageSize * (pageNo - 1)
	var adminLogs []*adminlogmodel.AdminLog
	sqlStr := "SELECT * FROM admin_log WHERE timestamp > ? and timestamp < ? LIMIT ?,?"
	err := dao.GMysqlDB.Conn.Select(&adminLogs, sqlStr, beginTime, endTime, start, pageSize)
	if err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return adminLogs
}

func GetAdminLogsByAdminID(pageSize int, pageNo int, beginTime string, endTime string, adminID int) []*adminlogmodel.AdminLog {
	start := pageSize * (pageNo - 1)
	var adminLogs []*adminlogmodel.AdminLog
	var adminName []*string
	err := dao.GMysqlDB.Conn.Select(&adminName, "SELECT name from admin WHERE id=?", adminID)
	if err != nil {
		logger.Logger.Error(err)
		return nil
	}
	sqlStr := "SELECT * FROM admin_log WHERE name = ? and timestamp > ? and timestamp < ? LIMIT ?,?"
	err = nil
	err = dao.GMysqlDB.Conn.Select(&adminLogs, sqlStr, *adminName[0], beginTime, endTime, start, pageSize)
	if err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return adminLogs
}

func GetAdminLogsByAdminName(pageSize int, pageNo int, beginTime string, endTime string, adminName string) []*adminlogmodel.AdminLog {
	start := pageSize * (pageNo - 1)
	var adminLogs []*adminlogmodel.AdminLog
	sqlStr := "SELECT * FROM admin_log WHERE name = ? and timestamp > ? and timestamp < ? LIMIT ?,?"
	err := dao.GMysqlDB.Conn.Select(&adminLogs, sqlStr, adminName, beginTime, endTime, start, pageSize)
	if err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return adminLogs
}

func GetAdminLogsCount(beginTime, endTime string) int {
	var count int
	sqlStr := "SELECT COUNT(*) from admin_log WHERE timestamp > ? and timestamp < ?"
	if err := dao.GMysqlDB.Conn.Get(&count, sqlStr, beginTime, endTime); err != nil {
		logger.Logger.Error(err)
		return -1
	}
	return count
}

func GetAdminLogsCountByAdminID(beginTime, endTime string, adminID int) int {
	var count int
	var adminName []*string
	err := dao.GMysqlDB.Conn.Select(&adminName, "SELECT name from admin WHERE id=?", adminID)
	if err != nil {
		logger.Logger.Error(err)
		return -1
	}
	sqlStr := "SELECT COUNT(*) from admin_log WHERE name=? and timestamp > ? and timestamp < ?"
	if err := dao.GMysqlDB.Conn.Get(&count, sqlStr, *adminName[0], beginTime, endTime); err != nil {
		logger.Logger.Error(err)
		return -1
	}
	return count
}

func GetAdminLogsCountByAdminName(beginTime, endTime string, adminName string) int {
	var count int
	sqlStr := "SELECT COUNT(*) from admin_log WHERE name=? and timestamp > ? and timestamp < ?"
	if err := dao.GMysqlDB.Conn.Get(&count, sqlStr, adminName, beginTime, endTime); err != nil {
		logger.Logger.Error(err)
		return -1
	}
	return count
}
